package ali;

import java.util.Stack;

/**
 *
 * @author wxh
 */
public class MyQueue {

	private Stack<Integer> stack1;
	private Stack<Integer> stack2;

	/** Initialize your data structure here. */
	public MyQueue() {
		stack1 = new Stack<>();
		stack2 = new Stack<>();
	}

	/** Push element x to the back of queue. */
	public void push(int x) {
		stack1.push(x);
	}

	/** Removes the element from in front of queue and returns that element. */
	public int pop() {
		return this.dequeue((stack)-> stack.pop());
	}

	/** Get the front element. */
	public int peek() {
		return this.dequeue((stack)-> stack.peek());
	}

	/** Returns whether the queue is empty. */
	public boolean empty() {
		return stack1.empty();
	}

	private int dequeue(StackOperate stackOperate) {
		//每次都用两个栈来回倒腾
		while (!stack1.empty()) {
			stack2.push(stack1.pop());
		}
		Integer value = stackOperate.doOperate(stack2);
		while (!stack2.empty()) {
			stack1.push(stack2.pop());
		}
		return value;
	}

	@FunctionalInterface
	interface StackOperate {
		int doOperate(Stack<Integer> stack);
	}

}